export function list2map(list) {
  const map = {}
  list.forEach(item => {
    map[item.value] = item.label
  })
  return map
}

// 首字母大写
export function convertName (str) {
  const [first, ...rest] = str
  return first.toUpperCase() + rest.join('')
}

const mapReg = /Map$/;
const treeReg = /Tree$/


export function enhanceState(obj) {
  const state = obj.state
//
  Object.keys(state).forEach(key => {

    // 生成getters
    obj.getters[key] = state => {
      return state[key];
    };

    const isNeedMap = !mapReg.test(key) && !treeReg.test(key)

    // 添加setter方法
    obj.mutations[`set${convertName(key)}`] = (state, payload) => {
      state[key] = [...payload];
      if(isNeedMap) {
        state[`${key}Map`] = list2map(payload);
      }
    };

    // map和树不需要添加 map了
    if(!isNeedMap) {
      return
    }
    // 添加key对应的map属性
    state[`${key}Map`] = {}

    if(state[key].length > 0) {
      state[`${key}Map`] = list2map(state[key])
    }


  });
}
